package ru.sstu.renju.javafx.renderers;

import javafx.scene.canvas.GraphicsContext;
import ru.sstu.renju.config.Config;
import ru.sstu.renju.core.CellType;
import ru.sstu.renju.core.Row;
import ru.sstu.renju.ui.GameRenderer;

/**
 * {@code FXRenderer} class is generic renderer for JavaFX.
 *
 * @author denis_murashev
 * @since Renju 1.1
 */
public abstract class FXRenderer implements GameRenderer {

	private final String name;
	private Config config;

	/**
	 * @param name renderer name
	 */
	public FXRenderer(String name) {
		this.name = name;
	}

	@Override
	public void setConfig(Config config) {
		this.config = config;
	}

	@Override
	public String toString() {
		return name;
	}

	/**
	 * Painting game field.
	 *
	 * @param g graphics context
	 */
	public abstract void paintField(GraphicsContext g);

	/**
	 * Painting cell.
	 *
	 * @param g    graphics context
	 * @param i    column index
	 * @param j    row index
	 * @param type cell type
	 */
	public void paintCell(GraphicsContext g, int i, int j, CellType type) {
		if (type == CellType.BLACK) {
			paintBlack(g, i, j);
		} else if (type == CellType.WHITE) {
			paintWhite(g, i, j);
		}
	}

	/**
	 * Painting winning row.
	 *
	 * @param g   graphics context
	 * @param row row
	 */
	public abstract void paintRow(GraphicsContext g, Row row);

	/**
	 * @return the game size
	 * @since Renju 1.1
	 */
	protected int getGameSize() {
		return config.getGameSize();
	}

	/**
	 * @return the row size
	 * @since Renju 1.1
	 */
	protected int getRowSize() {
		return config.getRowSize();
	}

	/**
	 * @return the cellSize
	 */
	protected int getCellSize() {
		return config.getCellSize();
	}

	/**
	 * Painting Black Stone (or Cross).
	 *
	 * @param g graphics context
	 * @param i column index
	 * @param j row index
	 */
	protected abstract void paintBlack(GraphicsContext g, int i, int j);

	/**
	 * Painting White Stone (or Nought).
	 *
	 * @param g graphics context
	 * @param i column index
	 * @param j row index
	 */
	protected abstract void paintWhite(GraphicsContext g, int i, int j);
}
